//
//  UserInfomationManager.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/21.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "UserInfomationManager.h"

@implementation UserInfomationManager

+(instancetype)shareManager{
    static UserInfomationManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[UserInfomationManager alloc] init];
    });
    return manager;
}
//更新用户信息
+(void)updateUserInfo:(NSDictionary *)userInfo{
    dispatch_async(dispatch_get_main_queue(), ^{
        [UserInfomationManager shareManager].balance = [userInfo[@"balance"] floatValue];
        [UserInfomationManager shareManager].credit = [userInfo[@"credit"] floatValue];
        [UserInfomationManager shareManager].userId = [userInfo[@"id"] integerValue];
        [UserInfomationManager shareManager].identity = [userInfo[@"identity"] integerValue];
        [UserInfomationManager shareManager].type = userInfo[@"type"];
        [UserInfomationManager shareManager].name = userInfo[@"name"];
        [UserInfomationManager shareManager].photo = userInfo[@"photo"];
        [UserInfomationManager shareManager].qrcode = userInfo[@"qrcode"];
    });
}
//退出登录
+(void)loginOut{
    [UserInfomationManager shareManager].token = @"";
    [UserInfomationManager shareManager].userId = 0;
    [UserInfomationManager shareManager].loginStatus = NO;
}

#pragma mark - setter
- (void)setToken:(NSString *)token{
    if ([self isEmptyWithString:token]==YES) {
        return;
    }
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setValue:token forKey:@"userToken"];
    [ud synchronize];
}
- (void)setQrcode:(NSString *)qrcode{
    if ([self isEmptyWithString:qrcode]==YES) {
        return;
    }
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setValue:qrcode forKey:@"userQrcode"];
    [ud synchronize];
}

- (void)setUserId:(NSInteger)userId{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setValue:@(userId) forKey:@"userId"];
    [ud synchronize];
}
- (void)setLoginStatus:(BOOL)loginStatus{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setValue:@(loginStatus) forKey:@"userLoginStatus"];
    [ud synchronize];
}
- (void)setBalance:(float)balance{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setValue:[NSString stringWithFormat:@"%f",balance] forKey:@"userBalance"];
    [ud synchronize];
}
- (void)setCredit:(float)credit{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setValue:[NSString stringWithFormat:@"%f",credit] forKey:@"userCredit"];
    [ud synchronize];
}
- (void)setIdentity:(NSInteger)identity{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setValue:@(identity) forKey:@"userIdentity"];
    [ud synchronize];
}
- (void)setType:(NSString *)type{
    if ([type isEmpty]==YES || type.length<=0) {
        return;
    }
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setValue:type forKey:@"userType"];
    [ud synchronize];
}
- (void)setName:(NSString *)name{
    if ([name isEmpty]==YES || name.length<=0) {
        return;
    }
    
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setValue:name forKey:@"userName"];
    [ud synchronize];
}
- (void)setPhoto:(NSString *)photo{
    if ([photo isEmpty]==YES || photo.length<=0) {
        return;
    }
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setValue:photo forKey:@"userPhoto"];
    [ud synchronize];
}

///判断字符串是否为空
-(BOOL)isEmptyWithString:(id)str{
    if (str == nil || str == NULL) {
        return YES;
    }
    if ([str isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([str isEqualToString:@"(null)"] || [str isEqualToString:@"null"]) {
        return YES;
    }
    return NO;
}

#pragma mark - getter
- (NSString *)token{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSString *token = [ud valueForKey:@"userToken"];
    return token;
}
- (NSInteger)userId{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSInteger userId = [[ud valueForKey:@"userId"] integerValue];
    return userId;
}
- (NSString *)qrcode{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSString *qrCode = [ud valueForKey:@"userQrcode"];
    return qrCode;
}
- (BOOL)loginStatus{
    return [[[NSUserDefaults standardUserDefaults] valueForKey:@"userLoginStatus"] boolValue];
}
- (float)balance{
    return [[[NSUserDefaults standardUserDefaults] valueForKey:@"userBalance"] floatValue];
}
- (float)credit{
    return [[[NSUserDefaults standardUserDefaults] valueForKey:@"userCredit"] floatValue];
}
- (NSInteger)identity{
    return [[[NSUserDefaults standardUserDefaults] valueForKey:@"userIdentity"] integerValue];
}
- (NSString *)type{
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"userType"];
}
- (NSString *)name{
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"userName"];
}
- (NSString *)photo{
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"userPhoto"];
}


@end
